
#include "uncategorized.h"

void _5404();
void _5453();
void _5507();
void _5564();
void _55E8();
void _5643();
void _5660();
void _5696();
void _56C2();

void _5404()
{
    C_Ram_Temp3 = 0x03;
    C_X_Loop = C_Ram_Temp4;
    if (C_X_Loop)
    {
    _5419:
        UINT8 x = C_Temp;
        C_Ram_Temp = MCU_memory[*(UINT16*)(MCU_memory + 0x38)] & C_Temp;
        C_Temp ^= 0xFF;
        C_Ram_Temp2 = (MCU_memory[*(UINT16*)(MCU_memory + 0x38)] ^ 0xFF) & C_Temp;
        MCU_memory[*(UINT16*)(MCU_memory + 0x38)] = C_Ram_Temp2 | C_Ram_Temp;
        C_Temp = x;
        C_X_Loop--;
        if (C_X_Loop)
        {
            _5507();
        }
        else
        {
            _5564();
        }
    }
    else
    {
        _5643();
    }
}

void _5453()
{
    C_Ram_Temp3 = 0x04;
    C_X_Loop = C_Ram_Temp4;
    if (C_X_Loop)
    {
    _5468:
        if ((Lcd_RamMapAddressMove == 0x01) && (*(UINT16*)(MCU_memory + 0x3A) == Lcd_RamMapOldAddress))
        {
        _547F:
            *(UINT16*)(MCU_memory + 0x3A) = Lcd_RamMapNewAddress;
            UINT8 x = C_Temp;
            C_Ram_Temp = MCU_memory[*(UINT16*)(MCU_memory + 0x3A)] & C_Temp;
            C_Temp ^= 0xFF;
            C_Ram_Temp2 = (MCU_memory[*(UINT16*)(MCU_memory + 0x3A)] ^ 0xFF) & C_Temp;
            MCU_memory[*(UINT16*)(MCU_memory + 0x3A)] = C_Ram_Temp2 | C_Ram_Temp;
            C_Temp = x;
            *(UINT16*)(MCU_memory + 0x3A) = Lcd_RamMapOldAddress;
        }
        else
        {
        _54C3:
            UINT8 x = C_Temp;
            C_Ram_Temp = MCU_memory[*(UINT16*)(MCU_memory + 0x3A)] & C_Temp;
            C_Temp ^= 0xFF;
            C_Ram_Temp2 = (MCU_memory[*(UINT16*)(MCU_memory + 0x3A)] ^ 0xFF) & C_Temp;
            MCU_memory[*(UINT16*)(MCU_memory + 0x3A)] = C_Ram_Temp2 | C_Ram_Temp;
            C_Temp = x;
        }
    _54EC:
        *(UINT16*)(MCU_memory + 0x3A) += 0x0001;
        C_X_Loop--;
        if (C_X_Loop)
        {
            _5507();
        }
        else
        {
            _5564();
        }
    }
    else
    {
        _5643();
    }
}

void _5507()
{
    if ((Lcd_RamMapAddressMove == 0x01) && (*(UINT16*)(MCU_memory + 0x3A) == Lcd_RamMapOldAddress))
    {
        _551E:
        *(UINT16*)(MCU_memory + 0x3A) = Lcd_RamMapNewAddress;
        MCU_memory[*(UINT16*)(MCU_memory + 0x3A)] ^= 0xFF;
        *(UINT16*)(MCU_memory + 0x3A) = Lcd_RamMapOldAddress;
    }
    else
    {
        _5541:
        MCU_memory[*(UINT16*)(MCU_memory + 0x3A)] ^= 0xFF;
    }
    _5549:
    *(UINT16*)(MCU_memory + 0x3A) += 0x0001;
    C_X_Loop--;
    if (C_X_Loop)
    {
        _5507();
    }
    else
    {
        _5564();
    }
}

void _5564()
{
    if ((Lcd_RamMapAddressMove == 0x01) && (*(UINT16*)(MCU_memory + 0x3A) == Lcd_RamMapOldAddress))
    {
        _557B:
        *(UINT16*)(MCU_memory + 0x3A) = Lcd_RamMapNewAddress;
        UINT8 x = C_Temp2;
        C_Ram_Temp = MCU_memory[*(UINT16*)(MCU_memory + 0x3A)] & C_Temp2;
        C_Temp2 ^= 0xFF;
        C_Ram_Temp2 = (MCU_memory[*(UINT16*)(MCU_memory + 0x3A)] ^ 0xFF) & C_Temp2;
        MCU_memory[*(UINT16*)(MCU_memory + 0x3A)] = C_Ram_Temp2 | C_Ram_Temp;
        C_Temp2 = x;
        *(UINT16*)(MCU_memory + 0x3A) = Lcd_RamMapOldAddress;
    }
    else
    {
        _55BF:
        UINT8 x = C_Temp2;
        C_Ram_Temp = MCU_memory[*(UINT16*)(MCU_memory + 0x3A)] & C_Temp2;
        C_Temp2 ^= 0xFF;
        C_Ram_Temp2 = (MCU_memory[*(UINT16*)(MCU_memory + 0x3A)] ^ 0xFF) & C_Temp2;
        MCU_memory[*(UINT16*)(MCU_memory + 0x3A)] = C_Ram_Temp2 | C_Ram_Temp;
        C_Temp2 = x;
    }
    _55E8();
}

void _55E8()
{
    *(UINT16*)(MCU_memory + 0x3A) = C_Point_Storage;
    _00E97985();
    C_Point_Storage = *(UINT16*)(MCU_memory + 0x3A);
    if (C_Ram_Temp3 == 0x01)
    {
    _5619:
        C_Y_Loop--;
        if (C_Y_Loop)
        {
            _5404();
        }
    }
    else if (C_Ram_Temp3 == 0x03)
    {
    _5627:
        C_Y_Loop--;
        if (C_Y_Loop)
        {
            _5404();
        }
    }
    else if (C_Ram_Temp3 == 0x04)
    {
    _5635:
        C_Y_Loop--;
        if (C_Y_Loop)
        {
            _5453();
        }
    }
}

void _5643()
{
    C_Temp |= C_Temp2;
    if (C_Ram_Temp3 == 0x01)
    {
        _5660();
    }
    else if (C_Ram_Temp3 == 0x02)
    {
        _56C2();
    }
    else if (C_Ram_Temp3 == 0x03)
    {
        _5696();
    }
    else if (C_Ram_Temp3 == 0x04)
    {
        _56C2();
    }
}

void _5660()
{
    UINT8 x = C_Temp;
    C_Ram_Temp = MCU_memory[*(UINT16*)(MCU_memory + 0x38)] & C_Temp;
    C_Temp ^= 0xFF;
    C_Ram_Temp2 = (MCU_memory[*(UINT16*)(MCU_memory + 0x38)] ^ 0xFF) & C_Temp;
    MCU_memory[*(UINT16*)(MCU_memory + 0x38)] = C_Ram_Temp2 | C_Ram_Temp;
    C_Temp = x;
    *(UINT16*)(MCU_memory + 0x38) = 0x03F3;
    _55E8();
}

void _5696()
{
    UINT8 x = C_Temp;
    C_Ram_Temp = MCU_memory[*(UINT16*)(MCU_memory + 0x38)] & C_Temp;
    C_Temp ^= 0xFF;
    C_Ram_Temp2 = (MCU_memory[*(UINT16*)(MCU_memory + 0x38)] ^ 0xFF) & C_Temp;
    MCU_memory[*(UINT16*)(MCU_memory + 0x38)] = C_Ram_Temp2 | C_Ram_Temp;
    C_Temp = x;
    _55E8();
}

void _56C2()
{
    if ((Lcd_RamMapAddressMove == 0x01) && (*(UINT16*)(MCU_memory + 0x3A) == Lcd_RamMapOldAddress))
    {
        _56D9:
        *(UINT16*)(MCU_memory + 0x3A) = Lcd_RamMapNewAddress;
        UINT8 x = C_Temp;
        C_Ram_Temp = MCU_memory[*(UINT16*)(MCU_memory + 0x3A)] & C_Temp;
        C_Temp ^= 0xFF;
        C_Ram_Temp2 = (MCU_memory[*(UINT16*)(MCU_memory + 0x3A)] ^ 0xFF) & C_Temp;
        MCU_memory[*(UINT16*)(MCU_memory + 0x3A)] = C_Ram_Temp2 | C_Ram_Temp;
        C_Temp = x;
        *(UINT16*)(MCU_memory + 0x3A) = Lcd_RamMapOldAddress;
    }
    else
    {
        _571D:
        UINT8 x = C_Temp;
        C_Ram_Temp = MCU_memory[*(UINT16*)(MCU_memory + 0x3A)] & C_Temp;
        C_Temp ^= 0xFF;
        C_Ram_Temp2 = (MCU_memory[*(UINT16*)(MCU_memory + 0x3A)] ^ 0xFF) & C_Temp;
        MCU_memory[*(UINT16*)(MCU_memory + 0x3A)] = C_Ram_Temp2 | C_Ram_Temp;
        C_Temp = x;
    }
    _55E8();
}

// 00E942D7 - 00E94749
UINT8 Cursor()
{
    if (Cursor_X >= LcdWidth)
    {
        return 0x00;
    }
    if (Cursor_X + Cursor_width >= LcdWidth+1)
    {
        return 0x00;
    }
    if (Cursor_Y >= LcdHigh)
    {
        return 0x00;
    }
    if (Cursor_Y + Cursor_high >= LcdHigh+1)
    {
        return 0x00;
    }

    _5301:
    Cursor_Y_Int_Loop_Var = Cursor_Y;
    _00E97831();
    C_Point_Storage = *(UINT16*)(MCU_memory + 0x3A);
    UINT8 x = Cursor_X & 0x07;
    C_Temp = 0x00;
    _5321:
    if (x)
    {
        do
        {
            C_Temp = (C_Temp >> 1) + 0x80;
            x--;
        } while (x);
    }
    else
    {
        _5331:
        C_Temp = 0x00;
    }
    _5336:
    C_Temp2 = (Cursor_X + Cursor_width - 0x01) & 0x07;
    x = 0x07 - C_Temp2;
    C_Temp2 = 0x00;
    _5351:
    if (x)
    {
        do
        {
            C_Temp2 = (C_Temp2 << 1) + 0x01;
            x--;
        } while (x);
    }
    else
    {
        _5361:
        C_Temp2 = 0x00;
    }
    _5366:
    C_Y_Loop = Cursor_high;
    C_Ram_Temp = Cursor_X >> 3;
    C_Ram_Temp2 = (Cursor_X + Cursor_width - 0x01) >> 3;
    _5385:
    if (C_Ram_Temp2 < C_Ram_Temp)
    {
        return 0x100 + C_Ram_Temp2 - C_Ram_Temp;
    }
    _538E:
    C_X_Loop = C_Ram_Temp2 - C_Ram_Temp;
    C_Ram_Temp4 = C_Ram_Temp2 - C_Ram_Temp;
    C_Ram_Temp3 = 0x00;
    _5399:
    if (Cursor_Y == 0x41)
    {
        _53A3:
        if (Cursor_X < 0x08)
        {
            _53AE:
            C_Ram_Temp3 = 0x01;
            C_X_Loop = C_Ram_Temp4;
            if (C_X_Loop)
            {
                _53C3:
                x = C_Temp;
                C_Ram_Temp = MCU_memory[*(UINT16*)(MCU_memory + 0x38)] & C_Temp;
                C_Temp ^= 0xFF;
                C_Ram_Temp2 = (MCU_memory[*(UINT16*)(MCU_memory + 0x38)] ^ 0xFF) & C_Temp;
                MCU_memory[*(UINT16*)(MCU_memory + 0x38)] = C_Ram_Temp2 | C_Ram_Temp;
                C_Temp = x;
                C_X_Loop--;
                if (C_X_Loop)
                {
                    _5507();
                }
                else
                {
                    _5564();
                }
            }
            else
            {
                _5643();
            }
        }
        else
        {
            _5453();
        }
    }
    else
    {
        _53FD:
        if (Cursor_X < 0x08)
        {
            _5404();
        }
        else
        {
            _5453();
        }
    }
    return 0x00;
}

// 00E9E565 - 00E9E56A
void DirectReadData_start()
{
    UINT8 x = Parameter0;
    UINT8 y = 0x00;
    while (x)
    {
        buff_point[y] = _DATA3;
        y++;
        x--;
    }
}

// 00E9DFA2 - 00E9E0F3
void SysStart()
{
    _PADR = 0xC0;
    _PAR = 0x3F;
    _PA = 0xFF;
    _PBDR = 0xEB;
    _PBR = 0x14;
    _PB = 0x3F;
    _PIR = 0xFF;
    _PIIE = 0x00;
    _PIWK = 0xFF;
    _PIMODE = 0x00;
    _IER |= 0x80;
    _PO = 0x00;
    _KEYCON1 = 0x08;
    _KEYCON2 = 0x00;
    _KEYCODE = 0x00;
    KeyBuffTop = 0x00;
    KeyBuffBottom = 0x00;
    KeyBouncingFlag = 0x00;
    KeyBouncingTimes = 0x00;
    KeyBouncingFirst = 0x00;

    DictSysFlag = 0x00000000;
    SaveScreenTimes = 0x00;
    AlmBuffer[0] = 0x00;
    AlmBuffer[1] = 0x00;
    AlmBuffer[2] = 0x00;
    AlmBuffer[3] = 0x00;
    AlmBuffer[4] = 0x00;

    Timer1Flag = 0x00;
    MelodyFlag = 0x00;

    AutoPowerOffValue = 0x04;
    AutoPowerOffCount = 0x04;
    _VOLCON = 0x83;
    _GTCON = 0x2F;
    _GTCTL = 0xEB;
    _TISR = 0x3F;
    _RTCSEC = 0x00;
    _RTCMIN = 0x00;
    _RTCHR = 0x00;
    _RTCDAYL = 0x00;
    _RTCDAYH = 0x00;
    _STCTCON |= 0x41;
    _CTLD = 0xF8;
    _IER |= 0x02;
    _ALMDAYH = 0x00;
    _ALMDAYL = 0x00;
    _ALMHR = 0x00;
    _ALMMIN = 0x01;
    _IER |= 0x01;
    _STCTCON |= 0x20;
    _ST1LD = 0xA6;
    _STCTCON |= 0x04;
    _TISR = 0xFE;
    _TIER |= 0x01;
    Timer1Count = 0x00;
    _MTCT = 0xC8;
    _TISR = 0xDF;
    SysLcd_Init();
    _00E9E2B6();
    _00E9E0F4();
    AppEnvInit();
    _AUDCON |= 0xC0;
    _TIER |= 0x20;
    MelodyFlag |= 0x01;
    _MTCT = 0xFE;
    _00E842CA();
}

// 00EA2A22 - 00EA2A61
void AppEnvInit()
{
    SysMemInit(MEM_HEAP_START, MEM_HEAP_SIZE);
    GuiInit();
    FileRepairFlash();
}

// 00E9474A - 00E94761
void SysLcd_Init()
{
    UINT8* p = LcdVar_start;
    for (UINT8 i = 0; i < 0x80; i++)
    {
        p[i] = 0x00;
    }
}

// 00E842CA - 00E842DA
void _00E842CA()
{
    SysLCDVoltage(0x01);
    _00E84253();
}